package app.com.myanr;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/9/12.
 */

public class ThreadActivity extends Activity {

    private TextView textView ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        textView = (TextView) findViewById(R.id.text);

        new MyThread().start(); //开启线程
    }

    public class MyThread extends Thread {

        //继承Thread类，并改写其run方法
        private final static String TAG = "My Thread ===> ";

        @Override
        public void run() {
            for(int i = 0; i<100; i++)
            {
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                /*不能在主线程更新Ui，所以报错
                android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.*/
//                textView.setText(Thread.currentThread().getName() + "i =  " + i);

                Log.e(TAG, Thread.currentThread().getName() + "i =  " + i);
            }
        }
    }
}
